package Pre.Work;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.function.BinaryOperator;
import java.util.stream.Stream;

public class Exam02 {
    //有一个整数集合，分别完成以下操作：
    //所有元素之和
    //最⼤值
    //最⼩值
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        Collections.addAll(list,10,4,24,15,18,8,36,45);
        Integer i= list.stream().reduce(new BinaryOperator<Integer>() {
            @Override
            public Integer apply(Integer integer, Integer integer2) {
                return integer + integer2;
            }
        }).get();
        System.out.println( "元素之和为：" + i);
        Comparator<Integer> c = new Comparator<>(){
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1 - o2 ;
            }
        };
        int s1 = list.stream().max(c).get();
        System.out.println("最大值为：" + s1);

        int s2 = list.stream().min(c).get();
        System.out.println("最小值为：" + s2);


        //=============================
        Stream<Integer> stream = list.stream();
//        stream.sorted().sorted().sorted().sorted(); // 中间方法要连续调用 负责可能会出现异常或者提示流已经被关闭
//        stream.sorted();
//        stream.reduce(null);
//        stream.max(null);
//        stream.min(null);
        Integer sum = list.stream().reduce((i1,i2) -> i1 + i2).get();
        Integer max = list.stream().max((i1, i2) -> i1 - i2).get();
        Integer min = list.stream().min((i1, i2) -> i1 - i2).get();
        System.out.println(sum);
        System.out.println(max);
        System.out.println(min);
    }
}
